<?php
/**
 * Dynamic menu  helper library.
 *
 * Methods to render dynamic menu .
 */
class MenuHelper extends AppHelper {
 
	var $helpers = array('Html');
 
	function render($menu = null,$display = null,$title=null,$filtercontroller=null,$id=null,$exclude=null,$message=null) {
		$out = '';
		if ($this->params['url']['url'] == "/")
		{
			$here = $this->params['url']['url'];
		}else{
			$here = "/".$this->params['url']['url'];
		}
		
		if (!empty($exclude))
			$menu=$this->__excludeController($menu,$exclude);
		
		if (!empty($filtercontroller))
				$menu=$this->__filterController($menu,$filtercontroller);
				
		
		foreach($menu as $caption => $action) {
			
			if (!is_array($action)){		
					$mytitle=$this->__getTitle($action,$title);
					$act=$this->__getHere($here,$action);
					
					
					if (!empty($mytitle)){
						$out = $out.$act.$this->Html->link(__($mytitle,true), $action);'</li>';
					}else{
						$out = $out.$act.$this->Html->link(__($caption,true), $action);'</li>';
					}
			}else{
				foreach ($action as $key => $value){
					$mytitle=$this->__getTitle($value,$title);
					$act=$this->__getHere($here,$value);
					
					if (!empty($display)){
						if (in_array($key,$display)){
							
							if (!empty($mytitle)){
								if (!empty($id)){
									$out = $out.$act.$this->Html->link(__($mytitle,true), array('controller'=>strtolower($caption),'action'=>$key,$id));'</li>';
								}else{
									$out = $out.$act.$this->Html->link(__($mytitle,true),$value);'</li>';
								}
							}else{
								if (!empty($id)){
									$out = $out.$act.$this->Html->link(__($caption,true), array('controller'=>strtolower($caption),'action'=>$key,$id));'</li>';
								}else{
									$out = $out.$act.$this->Html->link(__($caption,true), $value);'</li>';
								}
							}
							
						}
					}else{
							if (!empty($mytitle)){
								$out = $out.$act.$this->Html->link(__($mytitle,true), $value);'</li>';
							}else{
								$out = $out.$act.$this->Html->link(__($caption,true), $value);'</li>';
							}
					}
				}
			}
		}
		
		
		$out = '<ul>'.$out.'</ul>';
		$out = $this->Html->div('actions', $out);
		return $out;
	}
	
	function __filterController ($menu,$filter) {
		
		foreach ($menu as $key => $value){
			if (!in_array($key,$filter)){
				unset($menu[$key]);
			}
		}
		
		return $menu;
	}
	
	function __excludeController ($menu,$filter) {
		
		foreach ($menu as $key => $value){
			if (in_array($key,$filter)){
				unset($menu[$key]);
			}
		}
		
		return $menu;
	}
	
	function __getTitle ($action,$title) {
		
		if (!empty($title)) {
			$mytitle=array_search($action,$title);
		}else{
			$mytitle=null;
		}
		
		return $mytitle;
	}
	
	function __getHere ($here,$value) {
		
		if ($here == $value) {
			$act = '<li class="active">';
		}else{
			$act = '<li>';
		}
		
		return $act;
	}
	
	function __getMessage ($message,$action) {
		if (!empty($message)) {
			foreach ($message as $key => $value){
				if ($key==$action) $mymessage=$value;
				else $mymessage=null;
			}
		}else{
			$mymessage=null;
		}
		
		return $mymessage;
	}
	
	function rendertable($menu = null,$display = null,$title=null,$filtercontroller=null,$id=null,$exclude=null,$message=null) {
		$out = '';
		if ($this->params['url']['url'] == "/")
		{
			$here = $this->params['url']['url'];
		}else{
			$here = "/".$this->params['url']['url'];
		}
		
		if (!empty($filtercontroller))
				$menu=$this->__filterController($menu,$filtercontroller);
		
		
		foreach($menu as $caption => $action) {
			
			if (!is_array($action)){		
					$mytitle=$this->__getTitle($action,$title);
					$act=$this->__getHere($here,$action);
					
					if (!empty($mytitle)){
						$out = $out.$this->Html->link(__($mytitle,true), $action);
					}else{
						$out = $out.$this->Html->link(__($caption,true), $action);
					}
			}else{
				foreach ($action as $key => $value){
					$mytitle=$this->__getTitle($value,$title);
					$act=$this->__getHere($here,$value);
					$mymessage=$this->__getMessage($message,$key);
					
					if (!empty($display)){
						if (in_array($key,$display)){
							
							if (!empty($mytitle)){
								if (!empty($id)){
									$out = $out.$this->Html->link(__($mytitle,true), array('controller'=>strtolower($caption),'action'=>$key,$id),null,__($mymessage,true));
								}else{
									$out = $out.$this->Html->link(__($mytitle,true),$value,null,__($mymessage,true));
								}
							}else{
								if (!empty($id)){
									$out = $out.$this->Html->link(__($caption,true), array('controller'=>strtolower($caption),'action'=>$key,$id),null,__($mymessage,true));
								}else{
									$out = $out.$this->Html->link(__($caption,true), $value,null,__($mymessage,true));
								}
							}
							
						}
					}else{
							if (!empty($mytitle)){
								$out = $out.$this->Html->link(__($mytitle,true), $value,null,__($mymessage,true));
							}else{
								$out = $out.$this->Html->link(__($caption,true), $value,null,__($mymessage,true));
							}
					}
				}
			}
		}
		
		
		$out = '<td class="actions">'.$out.'</td>';
		return $out;
	}
	
}
?>